<template>
	<view class="normal-login-container">
		<view>
			<!-- <image src="/static/images/login/沟通.png"
				style="width: 400rpx;height: 400rpx;display: flex;float: right;margin-right: 40px;">
			</image> -->
		</view>
		<view>
			<view class="wave"></view>
			<view class="wave"></view>
			<view class="wave"></view>
		</view>
		<view>
			<view class="logo-content align-center justify-center ">
				<image style="width: 300rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
				</image>
				<br>
				<text class="title">OMS系统移动端</text>
			</view>
			<view class="login-form-content" style="margin-top: 200rpx;">
				<view class="input-item flex align-center">
					<view class="iconfont icon-user icon"></view>
					<input v-model="loginForm.principal" class="input" type="text" placeholder="请输入账号" maxlength="30" />
				</view>
				<view class="input-item flex align-center">
					<view class="iconfont icon-password icon"></view>
					<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码"
						maxlength="20" />
				</view>
				<!-- <view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
					<view class="iconfont icon-code icon"></view>
					<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
					<view class="login-code">
						<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
					</view>
				</view> -->
				<view class="action-btn">

					<button v-if="webType!=0" @click="handleLogin" plain type="primary" class="round">账号密码登录</button>
					<button v-if="webType==0" @click="handleLogin"
						class="login-btn cu-btn block bg-blue lg round">账号密码登录</button>

					<button v-if="webType==2" @click="ddLogin" type="primary"
						class="login-btn cu-btn block bg-blue lg round">
						钉钉登录</button>

					<button v-if="webType==1" @click="handleWeChatLogin"
						class="login-btn cu-btn block bg-green lg round"><uni-icons type="weixin" color="#fff" size="25"
							style="margin-right: 20rpx;"></uni-icons>微信登录</button>



				</view>
				<view class="reg text-center" v-if="register">
					<text class="text-grey1">没有账号？</text>
					<text @click="handleUserRegister" class="text-blue">立即注册</text>
				</view>
				<view class="xieyi text-center">
					<text class="text-grey1">登录即代表同意</text>
					<text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
					<text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	import {
		setcommon
	} from '@/utils/common'
	import {
		getAllUser,
		getUserBySource,
		switchOrg,
	} from "@/api/system/user/userManagement";

	import {
		jiaoshigetListNoPage
	} from "@/api/system/classroom/SysClassroomManagementApi";

	import {
		findAllByCurriculumList
	} from "@/api/system/curriculum/AxCurriculumManagementApi.js";
	import {
		findTreeAll
	} from "@/api/tenantSet/source";
	import {
		shijianduangetListNoPage
	} from "@/api/system/timeSlot/SysTimeSlotManagementApi";

	import {
		getToken,
		setToken,
		removeToken
	} from '@/utils/auth'
	import {
		GetCode,
		isDD
	} from "@/utils/dingding.js"
	import {
		isNull
	} from '../utils/validate'
	import {
		getCodeImg,
		callBackUrl,
		getAccess_tokenAdnOpenidByCode,
		ddGetUserByCode
	} from '@/api/login'
	import {
		uuid
	} from '@/utils/auth.js'
	import storage from '@/utils/storage'
	export default {
		data() {
			return {
				webType: 0, //0 浏览器 1 微信 2 钉钉
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: false,
				globalConfig: getApp().globalData.config,
				loginForm: {
					principal: "",
					password: "",
					// principal: "anna_deng",
					// password: "Bb123456.",
					verificationCode: "",
					uuid: "",
					loginFrom: "0"
				},
				datass: {
					corpid: "" //钉钉参数
				}

			}
		},
		created() {

			this.init2()

		},
		onLoad(data) {

			if (isDD()) {
				this.webType = 2
			}

			// this.datass = data

			// uni.showToast({
			// 	title: data.corpid
			// })
			// this.ddLogin()
			this.checkLogin()
		},

		methods: {
			checkLogin() {
				let token = getToken();

				if (!isNull(token)) {
					this.loginSuccess()
				}
			},

			async ddLogin() {

				if (isDD()) {
					this.webType = 2
					let adf = await GetCode()

					let corpid = this.getCodeFromUrl2("corpid", window.location.href)
					const {
						data
					} = await ddGetUserByCode({
						code: adf,
						corpid: corpid
					})

					await this.$store.dispatch('WeChatLogin', data)
					await this.$store.dispatch('getMenu')
					this.$modal.closeLoading()
					this.loginSuccess()
				}
			},
			getCodeFromUrl(url) {
				const searchParams = new URLSearchParams(url.split('?')[1]);
				return searchParams.get('code');
			},

			getCodeFromUrl2(code, url) {
				let searchParams = new URLSearchParams(url.split('?')[1]);

				searchParams = searchParams.get(code);
				searchParams = searchParams.replace(/[#\/]/g, ''); // 使用正则表达式替换所有的#字符为空字符串
				return searchParams
			},

			init2() {

				//检测当前是否是微信环境
				if (/MicroMessenger/i.test(navigator.userAgent)) {
					this.webType = 1
				} else {

				}

				let code = this.getCodeFromUrl(window.location.href)
				if (!isNull(code)) {
					this.getAccess_tokenAdnOpenidByCode2(code)
				}
			},
			// 用户注册
			handleUserRegister() {
				this.$tab.redirectTo(`/pages/register`)
			},
			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.img
					}
				})
			},

			handleWeChatLogin() {
				this.callBackUrl()
			},

			async callBackUrl() {
				var url =
					'https://api3.m.grasswave.cn/opsli-boot/h5/callBackUrl?url=https%3A%2F%2Fm.m.grasswave.cn&type=OMS'
				window.location.href = url
			},

			async getAccess_tokenAdnOpenidByCode2(code) {
				console.log("code" + code);
				const {
					data
				} = await getAccess_tokenAdnOpenidByCode({
					code: code,
					type: "OMS"
				})
				console.log(data.token);

				await this.$store.dispatch('WeChatLogin', data)
				await this.$store.dispatch('getMenu')
				this.$modal.closeLoading()
				this.loginSuccess()
				// this.loginForm.uuid = uuid()
				// await this.$store.dispatch('Login', this.loginForm)
				// await this.$store.dispatch('getMenu')
				// this.$modal.closeLoading()
				// this.loginSuccess()
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.principal === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
				} else {
					this.$modal.loading("登录中...")
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.loginForm.uuid = uuid()
				await this.$store.dispatch('Login', this.loginForm)
				await this.$store.dispatch('getMenu')
				this.$modal.closeLoading()
				this.loginSuccess()
			},
			// 登录成功后，处理函数
			async loginSuccess(result) {


				//获取所有用户
				getAllUser().then((res) => {
					setcommon("createUserNameList", res.data);
				});

				//获取来源
				findTreeAll().then((res) => {
					setcommon("sourceList", res.data)
				});

				//获取课包
				findAllByCurriculumList().then((res) => {
					setcommon("curriculumList", res.data);
				});

				//获取教室
				jiaoshigetListNoPage().then((res) => {
					setcommon("sysClassroomList", res.data);
				});

				//获取休息时间
				shijianduangetListNoPage().then((res) => {
					//先根据时间排序
					var aaaaa = res.data.sort((a, b) => {
						var asss = a.startTime.split(":");
						var bsss = b.startTime.split(":");
						return parseInt(asss[0] + asss[1]) - parseInt(bsss[0] + bsss[1]);
					});
					setcommon("shijianduan", aaaaa);
				});


				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index')
				})
			}
		}
	}
</script>



<style>
	page {
		margin: auto;
		font-family: -apple-system, BlinkMacSystemFont, sans-serif;
		overflow: auto;
		background: linear-gradient(315deg, rgb(255, 204, 102) 33%, rgb(0, 155, 218) 38%, rgb(253, 208, 0) 68%, rgb(124, 196, 30) 78%);
		animation: gradient 15s ease infinite;
		background-size: 400% 400%;
		background-attachment: fixed;
		height: 100%;
	}

	@keyframes gradient {
		0% {
			background-position: 0% 0%;
		}

		25% {
			background-position: 25% 25%;
		}

		50% {
			background-position: 100% 100%;
		}

		70% {
			background-position: 70% 70%;
		}

		100% {
			background-position: 0% 0%;
		}


	}

	.wave {
		background: rgb(255 255 255 / 25%);
		/* background: rgba(255, 255,255, .1); */
		border-radius: 1000% 1000% 0 0;
		position: fixed;
		width: 200%;
		height: 12em;
		animation: wave 10s -3s linear infinite;
		transform: translate3d(0, 0, 0);
		opacity: 0.8;
		bottom: 0;
		left: 0;
		z-index: -1;
	}

	.wave:nth-of-type(2) {
		bottom: -1.25em;
		animation: wave 18s linear reverse infinite;
		opacity: 0.8;
	}

	.wave:nth-of-type(3) {
		bottom: -2.5em;
		animation: wave 10s -1s reverse infinite;
		opacity: 0.9;
	}

	@keyframes wave {
		2% {
			transform: translateX(1);
		}

		25% {
			transform: translateX(-25%);
		}

		50% {
			transform: translateX(-50%);
		}

		75% {
			transform: translateX(-25%);
		}

		100% {
			transform: translateX(1);
		}
	}

	.asdf {
		width: 100%;
		text-align: center;
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
		margin: 0;
		color: #fff;
		font-size: 40px;
		font-weight: bold;
	}
</style>



<style lang="scss">
	// page {
	// 	background-color: #ffffff;
	// }

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				color: #fff;
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>